﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace ECONTACT.DataLayer
{
    public class HocSinhData
    {
        DataService m_HocSinhData = new DataService();

        public DataTable LayDsHocSinh()
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM HOCSINH");
            m_HocSinhData.Load(cmd);
            return m_HocSinhData;
        }

        public DataTable LayDsHocSinhTheoLop(String MaLop)
        {
            DataService m_DData = new DataService();
            SqlCommand cmd = new SqlCommand("sp_DanhSachHocSinhTheoLop");

            cmd.Parameters.Add("@MaLop", SqlDbType.Int).Value = Convert.ToInt32(MaLop);

            //-----------------------------------------------------------
            SqlParameter output = new SqlParameter("@Dem", SqlDbType.Int);
            output.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(output);
            //-----------------------------------------------------------

            m_DData.LoadProc(cmd);
            MessageBox.Show(output.Value.ToString()); // -- show bat loi
            return m_DData;
        }

        //public String LayDsHocSinhTheoLop1(String MaLop)
        //{
        //    try
        //    {
        //        SqlCommand cmd = new SqlCommand("sp_DanhSachHocSinhTheoLop");

        //        cmd.Parameters.Add("@MaLop", SqlDbType.Int).Value = Convert.ToInt32(MaLop);

        //        SqlParameter output = new SqlParameter("@Dem", SqlDbType.Int);
        //        output.Direction = ParameterDirection.Output;
        //        cmd.Parameters.Add(output);
                
        //        m_HocSinhData.LoadProc(cmd);
        //        MessageBox.Show(output.Value.ToString());
        //    }
        //    catch (System.Exception ex)
        //    {
        //        return ex.Message;
        //    }
        //    return "Success";
        //}

        public String LuuHocSinh(String TenHS, bool GioiTinh, DateTime NgaySinh, String NoiSinh, String DiaChi, String MaPH)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("sp_ThemHocSinh");

                cmd.Parameters.Add("@TenHS", SqlDbType.NVarChar).Value = TenHS;
                cmd.Parameters.Add("@GioiTinh", SqlDbType.Bit).Value = GioiTinh;
                cmd.Parameters.Add("@NgaySinh", SqlDbType.DateTime).Value = NgaySinh;
                cmd.Parameters.Add("@NoiSinh", SqlDbType.NVarChar).Value = NoiSinh;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = DiaChi;
                cmd.Parameters.Add("@MAPHUHUYNH", SqlDbType.Int).Value = Convert.ToInt32(MaPH);

                m_HocSinhData.LoadProc(cmd);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "Success";
        }

        public String UpdateHocSinh(String MaHS, String TenHS, bool GioiTinh, DateTime NgaySinh, String NoiSinh, String DiaChi, String MaPH)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("sp_CapNhatHocSinh");
                cmd.Parameters.Add("@MaHS", SqlDbType.VarChar).Value = MaHS;
                cmd.Parameters.Add("@TenHS", SqlDbType.NVarChar).Value = TenHS;
                cmd.Parameters.Add("@GioiTinh", SqlDbType.Bit).Value = GioiTinh;
                cmd.Parameters.Add("@NgaySinh", SqlDbType.DateTime).Value = NgaySinh;
                cmd.Parameters.Add("@NoiSinh", SqlDbType.NVarChar).Value = NoiSinh;
                cmd.Parameters.Add("@DiaChi", SqlDbType.NVarChar).Value = DiaChi;
                cmd.Parameters.Add("@MaPH", SqlDbType.Int).Value = Convert.ToInt32(MaPH);

                m_HocSinhData.LoadProc(cmd);
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
            return "Success";
        }

        public void DeleteHocSinh(String MaHS)
        {
            SqlCommand cmd = new SqlCommand("sp_XoaHocSinh");
            cmd.Parameters.Add("@MAHOCSINH", SqlDbType.Int).Value = Convert.ToInt32(MaHS);

            m_HocSinhData.LoadProc(cmd);
        }
    }
}
